<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cage:cage:add']"
        >新增笼位</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cage:cage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="cageId" />
      <el-table-column label="笼子尺寸" align="center" prop="cageSize" />
      <el-table-column label="更新日期" align="center" prop="cageDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cageDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="动物种属" align="center" prop="animalMsg" />
      <el-table-column label="笼位编号" align="center" prop="cageBy2" />
      <el-table-column label="动物编号" align="center" prop="cageBy3" />
      <el-table-column label="笼子使用状态" align="center" prop="typeMsg" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改笼子（库存）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      
        <el-form-item label="笼子大小" prop="cageId">
                <el-select v-model="form.cageSizeId" placeholder="请选择笼子大小">
                  <el-option 
                  v-for="(cage, index) in cageSizeList"
                    :key="index"
                    :label="cage.cageSize"
                    :value="cage.cageSizeId"
                  ></el-option>
                </el-select>
              </el-form-item>      
        
              <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="更新日期" prop="cageDate">
          <el-date-picker clearable
            v-model="form.cageDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {queryCageSize,queryCageType, listCage, getCage, delCage, addCage, updateCage } from "@/api/cage/cage";


export default {
  name: "Cage",
  data() {
    return {
      //所有笼子类型的
      cageSizeList:[],
      //所有的笼子使用状态
      cageTypeList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 笼子（库存）表格数据
      cageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        cageSizeId: null,
        cageNum: null,
        cageDate: null,
        cageDesc: null,
        cageBy1: null,
        cageBy2: null,
        cageBy3: null,
        cageTypeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.loadAllCageType();
    this.loadStaffFeedingRecord();
  },
  methods: {
         /** 查询所有的笼子尺寸信息 */
         loadStaffFeedingRecord(){
        this.loading = true;
        queryCageSize().then(response=>{
            this.cageSizeList = response.data;
          
            this.loading = false;
        });
      },
      /** 查询所有的笼子状态信息 */
      loadAllCageType(){
        this.loading = true;
        queryCageType().then(response=>{
            this.cageTypeList = response.data;
            this.loading = false;
        });
      },
    /** 查询笼子（库存）列表 */
    getList() {
      this.loading = true;
      listCage(this.queryParams).then(response => {
        this.cageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cageId: null,
        cageSizeId: null,
        cageNum: null,
        cageDate: null,
        cageDesc: null,
        cageBy1: null,
        cageBy2: null,
        cageBy3: null,
        cageTypeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cageId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加笼子（库存）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cageId = row.cageId || this.ids
      getCage(cageId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改笼子（库存）";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cageId != null) {
            updateCage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form)
            addCage(this.form).then(response => {
             
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cageIds = row.cageId || this.ids;
      this.$modal.confirm('是否确认删除笼子（库存）编号为"' + cageIds + '"的数据项？').then(function() {
        return delCage(cageIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cage/cage/export', {
        ...this.queryParams
      }, `cage_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
